package com.itqhy.service;

import com.itqhy.dao.UserMapper;
import com.itqhy.exception.UserException;
import com.itqhy.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource(name = "userMapper")
    private UserMapper userMapper;

    /**
     * 用户注册
     * @param user
     * @throws UserException
     */
    public void register(User user) throws UserException {
        int i=userMapper.register(user);

        if (i>0){
            System.out.println("注册成功");
        }else{
            throw new UserException("注册失败");
        }
    }
    /**
     * 用户登录
     * @param username
     * @param password
     */
    public User loginByUsernameAndPasswoerd(String username, String password) throws UserException {
        User user=userMapper.loginByUsernameAndPasswoerd(username,password);
        if (user==null){

            throw new UserException("用户名或密码不正确!");

        }
       return user;
    }
    /**
     * 修改用户信息
      * @param user
     */
    public void updateUserById(User user) throws UserException {

       int i= userMapper.updateUserById(user);
       if (i>0){
           System.out.println("用户修改信息成功");
       }else {

           throw new UserException("用户修改失败");

       }

    }
}
